package com.mk.plugin.ui;

import com.mk.plugin.model.ToolbarComponentModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StartSetting extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JCheckBox pageCheckBox;
    private JTextField listApiUrl;
    private JButton addButton;
    private JPanel toolbarPanel;
    private JTextField fieldInput;
    private int i = 0;

    OkButtonListener okButtonListener;

    private Map<String, Object> functionSettings = new HashMap<>();

    public StartSetting(OkButtonListener okButtonListener) {
        toolbarPanel.setLayout(new BoxLayout(toolbarPanel, BoxLayout.Y_AXIS));
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        addButton.addActionListener(e -> {
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.setSize(200, 30);

            String[] componentList = new String[]{"date range", "search", "select"};
            JComboBox componentComboBox = new JComboBox<>(componentList);
            p.add(componentComboBox, BorderLayout.WEST);

            JTextField fieldInput = new JTextField();
            fieldInput.setSize(100, 30);
            p.add(fieldInput, BorderLayout.CENTER);

            JButton deleteButton = new JButton("删除");
            deleteButton.addActionListener(e2 -> {
                toolbarPanel.remove(p);
                toolbarPanel.revalidate();
            });
            p.add(deleteButton, BorderLayout.EAST);

            toolbarPanel.add(p);
            toolbarPanel.revalidate();
        });

        this.okButtonListener = okButtonListener;
        buttonOK.addActionListener(e -> onOk());

        buttonCancel.addActionListener(e -> onCancel());

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOk() {
        List<ToolbarComponentModel> toolbarComponentModelList = Arrays.stream(toolbarPanel.getComponents()).map(c -> {
            JPanel jPanel = (JPanel) c;
            return new ToolbarComponentModel(((JComboBox) jPanel.getComponent(0)).getSelectedItem().toString(), ((JTextField) jPanel.getComponent(1)).getText());
        }).collect(Collectors.toList());

        functionSettings.put("toolbarComponentModelList", toolbarComponentModelList);
//        functionSettings.put("dateRange", dateRangeCheckBox.isSelected());
        functionSettings.put("page", pageCheckBox.isSelected());
        functionSettings.put("listApiUrl", listApiUrl.getText());
        functionSettings.put("needPage", pageCheckBox.isSelected());
        functionSettings.put("fieldListText", fieldInput.getText());
        this.okButtonListener.onClick(functionSettings);
        dispose();
    }
    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    private void deleteOne(int index) {

    }

    public interface OkButtonListener {
        void onClick(Map<String, Object> functionSettings);
    }

}
